#include "muduo/proto/codec.h"
#include "muduo/proto/dispatcher.h"
#include "request.pb.h"

#include "muduo/base/Logging.h"
#include "muduo/base/Mutex.h"
#include "muduo/net/EventLoop.h"
#include "muduo/net/TcpServer.h"
#include <iostream>
#include <memory>
#include <unordered_map>
class server
{
public:
    typedef std::shared_ptr<google::protobuf::Message> Messageptr;
    typedef std::shared_ptr<bit::AddRequest> AddRequestptr;
    typedef std::shared_ptr<bit::AddResponse> AddResponseptr;
    typedef std::shared_ptr<bit::TranslateRequest> TranslateRequestptr;
    typedef std::shared_ptr<bit::TranslateResponse> TranslateResponseptr;
    server(int port)
        : _server(&_event, muduo::net::InetAddress("0.0.0.0", port), "Server", muduo::net::TcpServer::kReusePort), _dispatcher(std::bind(&server::onUnknownMessage, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)), _code(std::bind(&ProtobufDispatcher::onProtobufMessage, &_dispatcher, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))
    {
        // 注册业务需求回调函数
        _dispatcher.registerMessageCallback<bit::TranslateRequest>(std::bind(&server::onTranslate,this,std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        _dispatcher.registerMessageCallback<bit::AddRequest>(std::bind(&server::onAdd,this,std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        //将protobuf协议处理作为muduo信息回调函数
        _server.setMessageCallback(std::bind(&ProtobufCodec::onMessage,&_code,std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        _server.setConnectionCallback(std::bind(&server::onConnection, this,std::placeholders::_1));

    }
    void start()
    {
        _server.start();
        _event.loop();
    }
    ~server()
    {
    }

private:
    void onAdd(const muduo::net::TcpConnectionPtr &conn, const AddRequestptr &req, muduo::Timestamp)
    {
        int num1=req->num1();
        int num2=req->num2();
        int result=num1+num2;
        bit::AddResponse as;
        as.set_result(result);
        _code.send(conn,as);
    }
     std::string translate(std::string &as)
    {
        static std::unordered_map<std::string, std::string> mmap = {
            {"Hello", "你好"},
            {"你好", "Hello"},
            {"World", "世界"},
            {"世界", "World"}
        };
        auto it=mmap.find(as);
        if(it!=mmap.end())
        {
            return it->second;
        }
        return "没听懂!!";
    }
    void onTranslate(const muduo::net::TcpConnectionPtr &conn, const TranslateRequestptr& req, muduo::Timestamp)
    {
        std::string str=req->msg();
        std::string res=translate(str);
        bit::TranslateResponse as;
        as.set_msg(res);
        _code.send(conn,as);
    }
    void onConnection(const muduo::net::TcpConnectionPtr &conn)
    {
        if(conn->connected())
        {
            LOG_INFO<<"新连接建立成功";
        }
        else{
            LOG_INFO<<"新连接关闭";
        }
    }
    void onUnknownMessage(const muduo::net::TcpConnectionPtr &conn, const MessagePtr &message, muduo::Timestamp)
    {
        LOG_INFO << "onUnknownMessage: " << message->GetTypeName();
        conn->shutdown();
    }

private:
    muduo::net::TcpServer _server;
    muduo::net::EventLoop _event;
    ProtobufDispatcher _dispatcher; // 事件分发器
    ProtobufCodec _code;            // 协议处理器
};
int main()
{
    server as(8080);
    as.start();
}